package com.random.fox.whatspay.greendao.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.random.fox.whatspay.bean.Cost;
import com.random.fox.whatspay.greendao.DaoSession;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

/**
 * 用于存储消费或收入
 * Created by 渠 on 2014/11/7.
 */
public class CostDao extends AbstractDao<Cost, Long> {
    public static final String TABLENAME = "cost";

    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Date = new Property(1, java.util.Date.class, "date", false, "DATE");
        public final static Property AccountId = new Property(2, Long.class, "accountId", false, "ACCOUNT_ID");
        public final static Property Cost = new Property(3, long.class, "cost", false, "COST");
        public final static Property Des = new Property(4, String.class, "des", false, "DES");
    }

    private DaoSession daoSession;

    public CostDao(DaoConfig config) {
        super(config);
    }

    public CostDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        db.execSQL("CREATE TABLE " + constraint + "'cost' (" + //
                "'_id' INTEGER PRIMARY KEY AUTOINCREMENT," + // 0: id //自增的，不去做绑定
                "'DATE' INTEGER," + // 1: date
                "'ACCOUNT_ID' INTEGER NOT NULL," +// 2: accountId
                "'COST' INTEGER NOT NULL," +//3：cost
                "'DES' TEXT);"); //4 des
    }

    /** Drops the underlying database table.*/
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'cost'";
        db.execSQL(sql);
    }

    @Override
    protected void bindValues(SQLiteStatement stmt, Cost cost) {
        stmt.clearBindings();

//        Long id = cost.getId();
//        if (id!=null){
//            stmt.bindLong(1,id);
//        }
        long date = cost.getDate().getTime();
        stmt.bindLong(2, date);
        long accountId = cost.getAccountId();
        stmt.bindLong(3, accountId);
        long money = cost.getCost();
        stmt.bindLong(4, money);
        String des = cost.getDes();
        stmt.bindString(5, des);

    }

    @Override
    protected void attachEntity(Cost entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    protected Cost readEntity(Cursor cursor, int offset) {
        Cost cost = new Cost();
        readEntity(cursor, cost, offset);
        return cost;
    }

    @Override
    protected Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }

    @Override
    protected void readEntity(Cursor cursor, Cost cost, int offset) {
        cost.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        cost.setDate(cursor.isNull(offset + 1) ? null : new java.util.Date(cursor.getLong(offset + 1)));
        cost.setAccountId(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        cost.setCost(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3));
        cost.setDes(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 4));
    }

    @Override
    protected Long updateKeyAfterInsert(Cost cost, long rowId) {
        cost.setId(rowId);
        return rowId;
    }

    @Override
    protected Long getKey(Cost cost) {
        if (cost != null) {
            return cost.getId();
        } else {
            return null;
        }
    }

    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }
}
